package com.yohann.algorithm;

import com.sun.org.apache.bcel.internal.generic.NEW;

import java.util.Stack;

/**
 * <p>
 * 单链表
 * </p>
 *
 * @author Yohann
 * @since 2020/9/22
 */
public class SingleLinkedListTest {
    public static void main(String[] args) {
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.addByOrder(new HeroNode(4,"宋江","及时雨"));
        singleLinkedList.addByOrder(new HeroNode(2,"卢俊义","玉麒麟"));
        singleLinkedList.addByOrder(new HeroNode(1,"吴用","智多星"));
        singleLinkedList.addByOrder(new HeroNode(1,"林冲","豹子头"));

        //修改
        singleLinkedList.update(new HeroNode(4,"林冲","豹子头"));
        singleLinkedList.display();
//
//        System.out.println(SingleLinkedList.getNodeLength(singleLinkedList.getHead()));
//        System.out.println(SingleLinkedList.getNode(singleLinkedList.getHead(),1));

        System.out.println("*******************");
//        SingleLinkedList.reversNode(singleLinkedList.getHead());
//        singleLinkedList.display();
        SingleLinkedList.reversePrint(singleLinkedList.getHead());
    }
}

/**
 * 单链表面试题
 * 1.求单链表节点个数
 * 2.查找单链表中的倒数第k个节点
 * 3.单链表的反转
 * 4.从尾到头打印单链表
 * 5.合并两个有序的单链表，合并后链表依然有序
 */
class SingleLinkedList{
    //初始化头结点
    private HeroNode head = new HeroNode(0,"","");

    public HeroNode getHead() {
        return head;
    }

    public void add(HeroNode heroNode){
        HeroNode temp = head;

        while (true){
            //头结点没有下一个指向
            if (temp.next == null){
                break;
            }

            //一直指向空节点才结束循环，即到达链表尾部
            temp = temp.next;
        }

        //在尾部加入节点
        temp.next = heroNode;
    }

    //排序插入链表
    public void addByOrder(HeroNode heroNode){
        HeroNode temp = head;
        boolean flag = false;

        while (true){
            //下一个节点是否为空
            if (temp.next == null) {
                break;
            }
            //下一个节点是否大于输入节点
            if (temp.next.no > heroNode.no){
                break;
            }
            //输入节点是否已存在
            else if (temp.next.no == heroNode.no){
                flag = true;
                break;
            }

            temp = temp.next;
        }

        //判断flag
        if (flag){
            System.out.println(heroNode.no+"已存在！");
        }else{
            //输入节点下一个节点改为该节点的下一节点
            heroNode.next = temp.next;
            //该节点下一个节点改为输入节点
            temp.next = heroNode;
        }
    }

    //修改
    public void update(HeroNode heroNode){
        HeroNode temp = head;
        boolean flag = false;

        while (true){
            temp = temp.next;

            if (temp == null){
                break;
            }

            if (temp.no == heroNode.no){
                flag = true;
                break;
            }
        }

        if (flag){
            temp.name = heroNode.name;
            temp.nickname = heroNode.nickname;
        }else {
            System.out.println("不存在"+heroNode.no+"节点");
        }
    }

    public void delete(int num){
        HeroNode temp = head;
        boolean flag = false;

        while (true){
            if (temp.next == null){
                break;
            }

            if (temp.next.no == num) {
                flag = true;
                break;
            }

            temp = temp.next;
        }

        if (flag){
            temp.next = temp.next.next;
        }else {
            System.out.println("不存在"+num+"节点");
        }
    }

    public void display(){
        if (head.next == null){
            System.out.println("链表为空！");
            return;
        }

        HeroNode temp = head.next;
        while (true){
            System.out.println(temp);

            if (temp.next == null){
                break;
            }
            temp = temp.next;
        }
    }

    //求单链表节点个数
    public static int getNodeLength(HeroNode head){
        int length = 0;

        if (head.next != null) {
            HeroNode current = head.next;

            while (current != null){
                length++;
                current = current.next;
            }
        }

        return length;
    }

    //查找单链表中的倒数第k个节点
    public static HeroNode getNode(HeroNode head, int k){
        if (head.next == null){
            System.out.println("链表为空！");
            return null;
        }

        int length = getNodeLength(head);
        int index = length - k;

        if (index > 0){
            for (int i = 1; i < index; i++) {
                head = head.next;
            }
        }

       return head;
    }

    //单链表的反转
    public static void reversNode(HeroNode head){
        if (head.next == null || head.next.next == null){
            System.out.println("不需要反转！");
            return;
        }

        HeroNode current = head.next;
        HeroNode next = null;
        HeroNode reverse = new HeroNode(0,"","");

        while (current != null){
            //将链表剩余部分暂存
            next = current.next;
            //current下一个节点指向新链表的最前端
            current.next = reverse.next;
            //将current连接到新的链表上
            reverse.next = current;
            //current后移
            current = next;
        }

        head.next = reverse.next;
    }

    //从尾到头打印单链表
    public static void reversePrint(HeroNode head){
        Stack<HeroNode> stack = new Stack<>();
        HeroNode current = head.next;
        while (current != null){
            stack.push(current);
            current = current.next;
        }

        while (stack.size() > 0 ){
            System.out.println(stack.pop());
        }
    }
}

class HeroNode{
    int no;
    String name;
    String nickname;
    HeroNode next;

    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}